package com.auz.datagenerator.publishing.tcpip.client;

import java.awt.FlowLayout;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;

/**
 * Simple client panel used to display the values generated on a graph
 * 
 * @author Aurelien Bonnel
 */
public class ClientPanel extends JPanel implements Runnable {

	private static final long serialVersionUID = -5720060076058746461L;
	private TimeSeries series;

	/**
	 * Constructor
	 */
	public ClientPanel() {
		setSize(400, 400);
		setLayout(new FlowLayout());
		
		ChartPanel panel = new ChartPanel(createSeries());
		add(panel);
	}
	
	/**
	 * Create the graph
	 * @return - the empty chart
	 */
	private JFreeChart createSeries(){
		series = new TimeSeries("Generated Data");
		TimeSeriesCollection dataset = new TimeSeriesCollection(series);
		JFreeChart chart = ChartFactory.createTimeSeriesChart(
	            "Generated Data", 
	            "Time", 
	            "Value",
	            dataset, 
	            true, 
	            true, 
	            false);
		XYPlot plot = chart.getXYPlot();
		plot.setDomainCrosshairVisible(false);
		plot.getDomainAxis().setAutoRange(true);
		plot.getDomainAxis().setFixedAutoRange(300000);
		plot.getRangeAxis().setAutoRange(true);
		return chart;
	}
	
	/**
	 * Adds a value to the chart
	 * @param _value - the value to add to the chart
	 */
	public void addValue(double _value) {
		series.add(new TimeSeriesDataItem(new Millisecond(), _value));
	}

	public void run() {
		setVisible(true);
	}
	
}
